{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "<a href=\"https://githubtocolab.com/gee-community/geemap/blob/master/docs/notebooks/63_charts.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1",
   "metadata": {},
   "source": [
    "**Chart Overview**\n",
    "\n",
    "Reference: https://developers.google.com/earth-engine/guides/charts_feature#column_chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import ee\n",
    "import geemap\n",
    "import geemap.chart as chart\n",
    "\n",
    "# from geemap import chart"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# geemap.update_package()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4",
   "metadata": {},
   "source": [
    "## Creating a chart from ee.FeatureCollection by feature\n",
    "\n",
    "Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturebyfeature"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "features = ee.FeatureCollection(\"projects/google/charts_feature_example\").select(\n",
    "    \"[0-9][0-9]_tmean|label\"\n",
    ")\n",
    "\n",
    "Map.addLayer(features, {}, \"Ecoregions\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.ee_to_df(features)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "xProperty = \"label\"\n",
    "yProperties = [str(x).zfill(2) + \"_tmean\" for x in range(1, 13)]\n",
    "\n",
    "labels = [\n",
    "    \"Jan\",\n",
    "    \"Feb\",\n",
    "    \"Mar\",\n",
    "    \"Apr\",\n",
    "    \"May\",\n",
    "    \"Jun\",\n",
    "    \"Jul\",\n",
    "    \"Aug\",\n",
    "    \"Sep\",\n",
    "    \"Oct\",\n",
    "    \"Nov\",\n",
    "    \"Dec\",\n",
    "]\n",
    "colors = [\n",
    "    \"#604791\",\n",
    "    \"#1d6b99\",\n",
    "    \"#39a8a7\",\n",
    "    \"#0f8755\",\n",
    "    \"#76b349\",\n",
    "    \"#f0af07\",\n",
    "    \"#e37d05\",\n",
    "    \"#cf513e\",\n",
    "    \"#96356f\",\n",
    "    \"#724173\",\n",
    "    \"#9c4f97\",\n",
    "    \"#696969\",\n",
    "]\n",
    "title = \"Average Monthly Temperature by Ecoregion\"\n",
    "xlabel = \"Ecoregion\"\n",
    "ylabel = \"Temperature\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "options = {\n",
    "    \"labels\": labels,\n",
    "    \"colors\": colors,\n",
    "    \"title\": title,\n",
    "    \"xlabel\": xlabel,\n",
    "    \"ylabel\": ylabel,\n",
    "    \"legend_location\": \"top-left\",\n",
    "    \"height\": \"500px\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "chart.feature_by_feature(features, xProperty, yProperties, **options)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/9xzsUxg.png)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "ecoregions = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "normClim = (\n",
    "    ee.ImageCollection(\"OREGONSTATE/PRISM/Norm81m\").toBands().select(\"[0-9][0-9]_tmean\")\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(ecoregions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": [
    "normClim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14",
   "metadata": {},
   "outputs": [],
   "source": [
    "fc = geemap.zonal_stats(\n",
    "    normClim, ecoregions, stat_type=\"MEAN\", scale=500, return_fc=True, verbose=False\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15",
   "metadata": {},
   "outputs": [],
   "source": [
    "geemap.ee_to_df(fc)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16",
   "metadata": {},
   "source": [
    "## Creating a chart from ee.FeatureCollection by property\n",
    "\n",
    "Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturebyproperty"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "features = ee.FeatureCollection(\"projects/google/charts_feature_example\").select(\n",
    "    \"[0-9][0-9]_ppt|label\"\n",
    ")\n",
    "\n",
    "Map.addLayer(features, {}, \"Features\")\n",
    "Map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = geemap.ee_to_df(features)\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "19",
   "metadata": {},
   "outputs": [],
   "source": [
    "keys = [str(x).zfill(2) + \"_ppt\" for x in range(1, 13)]\n",
    "values = [\n",
    "    \"Jan\",\n",
    "    \"Feb\",\n",
    "    \"Mar\",\n",
    "    \"Apr\",\n",
    "    \"May\",\n",
    "    \"Jun\",\n",
    "    \"Jul\",\n",
    "    \"Aug\",\n",
    "    \"Sep\",\n",
    "    \"Oct\",\n",
    "    \"Nov\",\n",
    "    \"Dec\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20",
   "metadata": {},
   "outputs": [],
   "source": [
    "xProperties = dict(zip(keys, values))\n",
    "seriesProperty = \"label\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "21",
   "metadata": {},
   "outputs": [],
   "source": [
    "options = {\n",
    "    \"title\": \"Average Ecoregion Precipitation by Month\",\n",
    "    \"colors\": [\"#f0af07\", \"#0f8755\", \"#76b349\"],\n",
    "    \"xlabel\": \"Month\",\n",
    "    \"ylabel\": \"Precipitation (mm)\",\n",
    "    \"legend_location\": \"top-left\",\n",
    "    \"height\": \"500px\",\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "22",
   "metadata": {},
   "outputs": [],
   "source": [
    "chart.feature_by_property(features, xProperties, seriesProperty, **options)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23",
   "metadata": {},
   "source": [
    "![](https://i.imgur.com/mS3vzHJ.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24",
   "metadata": {},
   "source": [
    "## Creating a chart from ee.FeatureCollection from a group of features\n",
    "Reference: https://developers.google.com/earth-engine/guides/charts_feature#uichartfeaturegroups"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "25",
   "metadata": {},
   "outputs": [],
   "source": [
    "Map = geemap.Map()\n",
    "\n",
    "features = ee.FeatureCollection(\"projects/google/charts_feature_example\")\n",
    "xProperty = \"label\"\n",
    "yProperty = \"01_tmean\"\n",
    "seriesProperty = \"warm\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "26",
   "metadata": {},
   "outputs": [],
   "source": [
    "options = {\n",
    "    \"title\": \"Average January Temperature by Ecoregion\",\n",
    "    \"colors\": [\"#cf513e\", \"#1d6b99\"],\n",
    "    \"xlabel\": \"Ecoregion\",\n",
    "    \"ylabel\": \"Jan temp (C)\",\n",
    "    \"legend_location\": \"top-right\",\n",
    "    \"height\": \"500px\",\n",
    "    \"labels\": [\"Warm\", \"Cold\"],\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "27",
   "metadata": {},
   "outputs": [],
   "source": [
    "chart.feature_groups(features, xProperty, yProperty, seriesProperty, **options)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
